package com.efast.dmom.aps.datahandlers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.Customer;
import com.efast.dmom.aps.dao.CustomerDao;
import com.efast.dmom.aps.entity.CustomerEntity;
import com.efast.dmom.aps.util.TimeUtil;

@Component
public class CustomerHandler{
	@Autowired
	private CustomerDao customerDao;
	
	
	public void save(Customer obj) {
		if(obj.getInnerData() != null) {
		obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
		obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//		obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowStatus(1);
		obj.getInnerData().setRowTrxId(1);
		customerDao.save(obj.getInnerData());
		}
	}

	public void update(Customer obj) {
		
		if(obj.getInnerData() != null){
			
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());		
			customerDao.update(obj.getInnerData());
		}
		
		
	}

	public void delete(Customer obj) {
		
		if(obj.getInnerData() != null){

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		//	obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());			
			customerDao.delete(obj.getInnerData());
		}
	}

	public Customer getById(long id) {
		Customer customer = new Customer();
		CustomerEntity customerEntity = customerDao.getById(id);
		customer.setInnerData(customerEntity);
		return customer;
	}

	public Customer getByName(String name) {
		Customer customer = new Customer();
		customer.setInnerData(customerDao.getByName(name));
		return customer;
	}
	
	public Customer getCustomerByCode(String customerCode) {
		Customer customer = new Customer();
		customer.setInnerData(customerDao.getCustomerByCode(customerCode));
		return customer;
	}

}
